package com.xnyzc.lhy.resource.controller.amap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.order.entity.order.OaDOrder;
import com.xnyzc.lhy.resource.entity.amap.*;
import com.xnyzc.lhy.resource.mapper.worksheet.OaDOrderMapper;
import com.xnyzc.lhy.resource.service.amap.IAmapService;
import com.xnyzc.lhy.resource.util.GouldMapUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/amap")
public class AmapController {

    @Autowired
    private IAmapService iAmapService;

    @Autowired
    OaDOrderMapper oaDOrderMapper;

    /**
     * 获取司机终端ID
     *
     * @return
     */
    @ApiOperation(value = "获取司机终端ID", notes = "获取司机终端ID")
    @PostMapping(value = "getAmapTid")
    public Long getAmapTid(@RequestBody AmapTidParam param) {
        return iAmapService.getAmapTid(param);
    }

    /**
     * 逆地理编码
     *
     * @param location 经纬度
     * @return
     */
    @ApiOperation(value = "逆地理编码", notes = "逆地理编码")
    @PostMapping(value = "getRegeo")
    public String getRegeo(@RequestParam("location") String location) {
        return iAmapService.getRegeo(location);
    }

    /**
     * 获取猎鹰服务轨迹ID
     *
     * @return
     */
    @ApiOperation(value = "获取猎鹰服务轨迹ID", notes = "获取猎鹰服务轨迹ID")
    @PostMapping(value = "/getAmapGpdId")
    public Long getAmapGpdId(@RequestBody AmapGpsIdParam param) {
        return iAmapService.getAmapGpdId(param);
    }

    /**
     * 获取猎鹰服务轨迹ID
     *
     * @return
     */
    @ApiOperation(value = "获取猎鹰服务轨迹ID", notes = "获取猎鹰服务轨迹ID")
    @PostMapping(value = "/getAmapGpdIds")
    public Long getAmapGpdId(@RequestParam("sid") String sid, @RequestParam("tid") String tid) {
        return iAmapService.getAmapGpdId(sid, tid);
    }

    /**
     * 获取司机终端ID
     *
     * @return
     */
    @ApiOperation(value = "获取司机终端ID", notes = "获取司机终端ID")
    @PostMapping(value = "/getAmapTids")
    public Long getAmapTid(@RequestParam("serviceId") String serviceId, @RequestParam("userId") Long userId, @RequestParam("clientId") String clientId, @RequestParam("tag") String tag) {
        return iAmapService.getAmapTid(serviceId, userId, clientId, tag);
    }


    /**
     * 获取两点之间的距离
     *
     * @return
     */
    @ApiOperation(value = "获取两点之间的距离", notes = "获取两点之间的距离")
    @GetMapping(value = "getDistance")
    public Double getDistance(
            String startLng, String startLat, String endLng, String endLat
    ) {
        return iAmapService.getDistance(startLng, startLat, endLng, endLat);
    }

    /**
     * 获取两点之间的距离与时间(文字版)
     *
     * @return
     */
    @ApiOperation(value = "获取两点之间的距离与时间(文字版)", notes = "获取两点之间的距离与时间(文字版)")
    @GetMapping(value = "getDistanceAndDuration")
    public HashMap<String, String> getDistanceAndDuration(String startPoint, String endPoint) {

        log.info("-------------------------------------------------------------------------------------------------------------------");
        log.info("起始经纬度：{}，终到经纬度：{}", startPoint, endPoint);
        log.info("-------------------------------------------------------------------------------------------------------------------");

        return iAmapService.getDistanceAndDuration(startPoint, endPoint);
    }

    /**
     * 获取两点之间的距离与时间(数字版)
     *
     * @return
     */
    @ApiOperation(value = "获取两点之间的距离与时间(数字版)", notes = "获取两点之间的距离与时间(数字版)")
    @GetMapping(value = "getStrategy")
    public Map<String, Integer> getStrategy(String startPoint, String endPoint) {
        return iAmapService.getStrategy(startPoint, endPoint);
    }

    /**
     * 根据开始与结束时间获取轨迹信息
     *
     * @return
     */
    @ApiOperation(value = "根据开始与结束时间获取轨迹信息", notes = "根据开始与结束时间获取轨迹信息")
    @PostMapping(value = "getHistoryGps")
    public AmapGouldHistoryGps getHistoryGps(
            @RequestParam("serviceId") Long serviceId, @RequestParam("tid") Long tid,
            @RequestParam("gpsId") Long gpsId, @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime, @RequestParam("page") int page, @RequestParam("type") Integer type
    ) {
        return GouldMapUtil.historyGps(serviceId, tid, gpsId, startTime, endTime, page, type);
    }

    @ApiOperation(value = "删除轨迹ID", notes = "删除轨迹ID")
    @PostMapping(value = "deleteGpsHistorys")
    public Boolean deleteGpsHistorys(@RequestBody UploadGpsParam param) {
//        return GouldMapUtil.deleteGpsHistorys(serviceId, gpsId, tid);
        return true;
    }

    /**
     * 获取两点之间的直线距离
     *
     * @return
     */
    @PostMapping(value = "uploadTrack")
    public Boolean uploadTrack(UploadGpsParam param) {
        return GouldMapUtil.uploadTrack(param.getSid(), param.getTid(), param.getTrid(), null);
    }

    /**
     * 根据开始与结束时间获取轨迹信息
     *
     * @return
     */
    @ApiOperation(value = "根据开始与结束时间获取轨迹信息", notes = "根据开始与结束时间获取轨迹信息")
    @PostMapping(value = "getDistancePoint")
    public AmapGouldResult getDistancePoint(
            String startPoint,
            String endPoint
    ) {
        return iAmapService.getDistancePoint(startPoint, endPoint);
    }
    /**
     * 上传mongo里点位数据
     *
     * @return
     */
    @PostMapping(value = "uploadTrackFromMongo")
    public Boolean uploadTrackFromMongo(@RequestBody OaDOrder oaDOrder) {
        return iAmapService.uploadTrackFromMongo(oaDOrder);
    }

    /**
     * 批量上传点位
     */
    @PostMapping(value = "uploadTrackFromMongoBatch")
    public int uploadTrackFromMongoBatch(@ApiParam("开始时间") @RequestParam String startTime,@ApiParam("结束时间") @RequestParam String endTime) {
        //查询数据库数据
        QueryWrapper<OaDOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.between(OaDOrder.FINISH_TIME,startTime,endTime);
        queryWrapper.isNull(OaDOrder.TRACK_ID);
        queryWrapper.eq(OaDOrder.ORDER_STATUS,12);
        List<OaDOrder> oaDOrders = oaDOrderMapper.selectList(queryWrapper);
        int count = 0 ;
        for (int i = 0; i < oaDOrders.size(); i++) {
            OaDOrder oaDOrder =oaDOrders.get(i);
            if (iAmapService.uploadTrackFromMongo(oaDOrder)){
                count++;
            }
        }
        return count;
    }
    /**
     * 获取两点之间的直线距离
     *
     * @return
     */
    @ApiOperation(value = "获取两点之间的距离", notes = "获取两点之间的距离")
    @GetMapping(value = "getLineDistance")
    public Integer getLineDistance(
            String startLng, String startLat, String endLng, String endLat
    ) {
        return iAmapService.getLineDistance(startLng, startLat, endLng, endLat);
    }


}
